//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.zip.InflaterInputStream;

public class Utils {
  public Utils() {
  }

  public static int readIntFromArray(byte[] stream, int offset) {
    return (stream[offset] << 24 & -16777216) + (stream[offset + 1] << 16 & 16711680) + (stream[offset + 2] << 8 & '\uff00') + (stream[offset + 3] << 0 & 255);
  }

  public static byte[] getDataBeforeHomeData(byte[] LogicAvatar) {
    int end = LogicAvatar.length;

    int i;
    int cl;
    int curLen;
    for(i = 0; i < end - 5; ++i) {
      cl = readIntFromArray(LogicAvatar, i);
      curLen = end - (i + 4);
      if(cl == curLen) {
        end = i;
        int compressedOffset = i + 8;
        int compressedLen = curLen - 4;
        break;
      }
    }

    if(end > 0) {
      for(i = 0; i < end - 5; ++i) {
        cl = readIntFromArray(LogicAvatar, i);
        curLen = end - (i + 4);
        if(cl == curLen) {
          byte[] rval = new byte[i];
          System.arraycopy(LogicAvatar, 0, rval, 0, i);
          return rval;
        }
      }
    }

    return null;
  }

  public static decompressedData tryToDecompressData(byte[] LogicAvatar) {
    decompressedData d = new decompressedData();
    int end = LogicAvatar.length;

    int baos;
    int e;
    int buffer;
    for(baos = 0; baos < end - 5; ++baos) {
      e = readIntFromArray(LogicAvatar, baos);
      buffer = end - (baos + 4);
      if(e == buffer) {
        end = baos;
        d.compressedOffset = baos + 8;
        d.compressedLen = buffer - 4;
        break;
      }
    }

    if(end > 0) {
      for(baos = 0; baos < end - 5; ++baos) {
        e = readIntFromArray(LogicAvatar, baos);
        buffer = end - (baos + 4);
        if(e == buffer) {
          d.compressedOffset = baos + 8;
          d.compressedLen = buffer - 4;
          break;
        }
      }
    }

    ByteArrayOutputStream var10 = new ByteArrayOutputStream();
    if(d.compressedLen > 0) {
      InflaterInputStream var11 = new InflaterInputStream(new ByteArrayInputStream(LogicAvatar, d.compressedOffset, d.compressedLen));
      byte[] var12 = new byte[8192];

      try {
        int len;
        while((len = var11.read(var12)) > 0) {
          var10.write(var12, 0, len);
        }

        var10.flush();
      } catch (Exception var9) {
        ;
      }
    }

    if(var10.size() > 0) {
      try {
        d.resultString = new String(var10.toByteArray(), "UTF-8");
      } catch (UnsupportedEncodingException var8) {
        var8.printStackTrace();
      }
    }

    return d;
  }

  public static void logException(Exception ex, String head) {
    if(ex.getMessage() == null) {
      System.out.println(head);
    } else {
      System.out.println(head + ex.getMessage());
    }

    StackTraceElement[] ts = ex.getStackTrace();
    StackTraceElement[] var6 = ts;
    int var5 = ts.length;

    for(int var4 = 0; var4 < var5; ++var4) {
      StackTraceElement e = var6[var4];
      System.out.println("          " + e.toString());
    }

  }

  public static void asyncExec(Runnable execo) {
    (new Thread(execo)).start();
  }

  public static void threadJoin(Thread pthr) {
    if(pthr != null) {
      while(true) {
        try {
          if(pthr != null) {
            pthr.interrupt();
            pthr.join();
          }
          break;
        } catch (Exception var2) {
          System.err.println(var2.getMessage());
        }
      }
    }

  }

  public static int secondsToObjectTicks(int seconds) {
    long sl = (long)seconds * 303L;
    return (int)(sl / 20L);
  }

  public static boolean canUse(int goodid, int amount, LinkedHashMap<Integer, Integer> inventory, boolean fieldsForceUse) {
    Integer fieldsReserved = Integer.valueOf(0);
    if(GlobalID.getClassID(goodid) == 4 && !fieldsForceUse && GlobalID.getInstanceID(goodid) == 1) {
      fieldsReserved = Integer.valueOf(2);
    }

    Integer have = (Integer)inventory.get(Integer.valueOf(goodid));
    if(have == null) {
      have = Integer.valueOf(0);
    }

    return amount + fieldsReserved.intValue() <= have.intValue();
  }
}
